import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
// const baseUrlWorkOrder = CommonConfig.planningUrl + '/v1'
const baseUrlTpm = CommonConfig.tpmUrl + '/v1'
// const baseUrlUpms = CommonConfig.upmsUrl + '/v1'
// const baseUrlProcess = CommonConfig.processUrl + '/v1'
// const baseUrlWorkOrder2 = CommonConfig.planningUrl + '/v3'
const baseUrlSchema = CommonConfig.schemaUrl + '/v1'
// const datacenterUrl = CommonConfig.datacenterUrl + '/v1'
import qs from 'qs'
const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// devices
// 列举设备
export function getDeviceIds (params) {
  const newUrl = `${baseUrlTpm}/devices`
  // const status = {
  //   level: 0,
  //   schemeId
  // }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}
// 模糊搜索设备下拉
export function getDeviceList (params) {
  const reqData = {
    params: {
      ...params,
      pageNum: 1,
      pageSize: 1000
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  const newUrl = `${baseUrlTpm}/devices/select/bySchemeAndDevice`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// v1/shiftsList/v1/workshop/{id}/shifts/use
// 根据车间ID 获取车间在用班制
// 根据车间ID list 获取班制
export function getShiftsList (workshopId) {
  const newUrl = `${baseUrlSchema}/workshop/${workshopId}/shifts/use`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// GET
// /v1/maintenance_order/calendar
// 保养日历查询
export function getCalendarList ({ deviceType, endDate,
  startDate, workshopId }) {
  const newUrl = `${baseUrlTpm}/maintenance_order/calendar`
  const params = {
    deviceType: deviceType === 'all' ? null : deviceType,
    endDate,
    startDate,
    workshopId
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}
// GET
// /v1/maintenance_schedule:page
// 列举保养计划 (分页)
export function getScheduleList ({ deviceType, programId, maintenanceType, pageNum, pageSize, recurRuleFreq
  , orderBy, workshopId, scheduleNo }) {
  const newUrl = `${baseUrlTpm}/maintenance_schedule:page`
  const params = {
    deviceType: deviceType === 'all' ? null : deviceType,
    programId: programId === 'all' ? null : programId,
    maintenanceType: maintenanceType === 'all' ? null : maintenanceType,
    recurRuleFreq: recurRuleFreq === 'all' ? null : recurRuleFreq,
    pageSize,
    orderBy,
    pageNum,
    scheduleNo,
    workshopId: workshopId === 'all' ? null : workshopId
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}
// /v1/maintenance_schedule/{id}
// 获取保养计划详情信息
export function getScheduleById (id) {
  const newUrl = `${baseUrlTpm}/maintenance_schedule/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// PUT
// /v1/maintenance_schedule/{id}
// 修改保养计划
export function editMaintenanceSchedule (id, params) {
  const newUrl = `${baseUrlTpm}/maintenance_schedule/${id}`
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

// /v1/maintenance_schedule
// 创建保养计划
export function addMaintenaceSchedule (params) {
  const newUrl = `${baseUrlTpm}/maintenance_schedule`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// DELETE
// /v1/maintenance_schedule/{id}
// 删除保养计划
export function deleteScheduleById (id) {
  const newUrl = `${baseUrlTpm}/maintenance_schedule/${id}`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

// PUT
// /v1/maintenance_schedule/{id}/enabled_status
// 修改保养计划
export function editMaintenanceEnabledStatus ({ id, status }) {
  const newUrl = `${baseUrlTpm}/maintenance_schedule/${id}/enabled_status?status=${status}`
  return axiosInstance.put(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// GET
// /v1/maintenance_schedule/{scheduleId}/devices
// 根据保养计划ID列举设备
export function getDeviceByScheduleId (scheduleId) {
  const newUrl = `${baseUrlTpm}/maintenance_schedule/${scheduleId}/devices`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
